package com.beyond.util;

import java.text.Format;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {


	 private final static long minute = 60 * 1000;// 1分钟

	 private final static long hour = 60 * minute;// 1小时

	 private final static long day = 24 * hour;// 1天

	 private final static long month = 31 * day;// 月

	 private final static long year = 12 * month;// 年



	 /**

	  * 返回文字描述的日期

	  *

	  * @param date

	  * @return

	  */

	 public static String getTimeFormatText(Date date) {
	     if (date == null) {
	         return null;

	     }

	     long diff = new Date().getTime() - date.getTime();

	     long r = 0;

	     if (diff > year) {

	         r = (diff / year);

	         return r + "年前";

	     }

	     if (diff > month) {

	         r = (diff / month);

	         return r + "个月前";

	     }

	     if (diff > day) {

	         r = (diff / day);

	         return r + "天前";

	     }

	     if (diff > hour) {

	         r = (diff / hour);

	         return r + "个小时前";

	     }

	     if (diff > minute) {

	         r = (diff / minute);

	         return r + "分钟前";

	     }

	     return "刚刚";

	 }
	 
	 public static void dateDiff(String startTime, String endTime, String format) {
		//按照传入的格式生成一个simpledateformate对象
		SimpleDateFormat sd = new SimpleDateFormat(format);
		long nd = 1000*24*60*60;//一天的毫秒数
		long nh = 1000*60*60;//一小时的毫秒数
		long nm = 1000*60;//一分钟的毫秒数
		long ns = 1000;//一秒钟的毫秒数
		long diff;
		try {
		//获得两个时间的毫秒时间差异
		diff = sd.parse(endTime).getTime() - sd.parse(startTime).getTime();
		long day = diff/nd;//计算差多少天
		long hour = diff%nd/nh;//计算差多少小时
		long min = diff%nd%nh/nm;//计算差多少分钟
		long sec = diff%nd%nh%nm/ns;//计算差多少秒
		//输出结果
		System.out.println("时间相差："+day+"天"+hour+"小时"+min+"分钟"+sec+"秒。");
		} catch (ParseException e) {
		e.printStackTrace();
		}
		}

	 	/**
	 	 * 当前时间的前几天天数
	 	 * @param c
	 	 * @param day
	 	 * @return
	 	 */
	 	public static Calendar test(Calendar c, int day) {
			c.add(Calendar.DATE, day);
			return c;
		}
	 	
	 	/**
	 	 * 当前时间的前几小时
	 	 * @param c
	 	 * @param day
	 	 * @return
	 	 */
	 	public static Calendar getHour(Calendar c, int hour) {
			c.add(Calendar.HOUR, hour);
			return c;
		}
	 	
	 	/**
	 	 * 当前时间的前minute
	 	 * @param c
	 	 * @param day
	 	 * @return
	 	 */
	 	public static Calendar getMinute(Calendar c,int minute){
	 		c.add(Calendar.MINUTE, minute);
			return c;
	 	}
	 	
	 	/**
	 	 * 根据当前时间获取对应范围的分钟
	 	 * @return
	 	 */
	 	public static String getMinute(){
	 		int minute = new Date().getMinutes();
	 		String val = "00";
	 		if(minute<=15)
	 			val = "15";
	 		else if(minute <= 30)
	 			val = "30";
	 		else if(minute <=45)
	 			val = "45";
	 		else 
	 			val = "00";
	 		
	 		return val;
	 	}
	 	
	 	/**
	 	 * 根据时间标示，获取对应时间格式时间
	 	 * @param tag
	 	 * @return
	 	 */
	 	public static String getNowTime(String tag){
	 		String dateFormate = "";
	 		if("15m".equals(tag)){
	 			dateFormate = "yyyyMMddHH";
	 		}else if("h".equals(tag)){
	 			dateFormate = "yyyyMMddHH";
	 		}if("d".equals(tag)){
	 			dateFormate = "yyyyMMdd";
	 		}
	 		Format f = new SimpleDateFormat(dateFormate);
			Calendar c = Calendar.getInstance();
			String time = f.format(DateUtil.getHour(c, Constant.Pre_HOUR_TIME).getTime());
			if("15m".equals(tag))
				time +=getMinute();
			
			return time;
	 	}
	 	
	 
	 public static void main(String[] args) {
		  	Format f = new SimpleDateFormat("yyyy-MM-dd HH:mm:dd");
		  	Format fm = new SimpleDateFormat("yyyy-MM-dd HH:mm:dd");
		  	
			Calendar c = Calendar.getInstance();

//			System.out.println("当前时间:" + f.format(new Date()));

			// 前一天
			c = test(c, -1);
//			System.out.println("  前一天:" + f.format(c.getTime()));

			// 后两天
			test(c, 2);
//			System.out.println("前6小时:" + f.format(getHour(c,-6).getTime()));
			System.out.println(getNowTime("15m"));
			
		}
}
